<?php

namespace App\Model;

use Carbon\Carbon;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property integer $id
 * @property integer $user_id 用户ID
 * @property string $title 活动标题
 * @property string $content 活动内容
 * @property integer $user_limit 人数限制
 * @property string $sign_rules 签到规则
 * @property string $sign_fields 自定义字段
 * @property integer $sign_type 签到类型
 * @property integer $allow_later_sign 是否允许补签
 * @property integer $status 状态
 * @property integer $is_publish 是否发布
 * @property \Carbon\Carbon $start_time 活动开始时间
 * @property \Carbon\Carbon $end_time 活动结束时间
 * @property \Carbon\Carbon $sign_in_start 签到开始时间
 * @property \Carbon\Carbon $sign_in_end 签到结束时间
 * @property \Carbon\Carbon $sign_out_start 签退开始时间
 * @property \Carbon\Carbon $sign_out_end 签退结束时间
 * @property \Carbon\Carbon $supplementary_start 补签开始时间
 * @property \Carbon\Carbon $supplementary_end 补签结束时间
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property \Carbon\Carbon $deleted_at
 */
class Meeting extends Model
{
    const MEETING_WAIT = 0; //未开始
    const MEETING_BEGINING = 1;//进行中
    const MEETING_FINISH = 2;//已结束
    const MEETING_ALLOW_LATER = 1; //允许补签
    const MEETING_NO_ALLOW_LATER = 0;//不允许补签
    const MEETING_PUBLISH = 1;//发布
    const MEETING_NO_PUBLISH = 0;//未发布

    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'meeting';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'user_id', 'title', 'content', 'user_limit', 'sign_type','allow_later_sign', 'sign_rules',
                           'sign_fields', 'status', 'is_publish', 'sign_in_start', 'sign_in_end', 'sign_out_start',  'sign_out_end',
                           'supplementary_start', 'supplementary_end', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['user_limit'=>'integer', 'sign_type'=>'integer', 'allow_later_sign'=>'integer', 'status'=>'integer','is_publish'=>'integer',
        'sign_in_start' => 'datetime','sign_in_end' => 'datetime','sign_out_start' => 'datetime','sign_out_end' => 'datetime',
        'supplementary_start' => 'datetime','supplementary_end' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at'=>'datetime'];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function isAllowSign(int $type, ?Carbon $now = null)
    {
        if ($now == null) {
            $now = Carbon::now();
        }
        if ($type == Sign::TYPE_IN) {
            if ($this->sign_in_start <= $now->getTimestamp() && $this->sign_in_end >= $now->getTimestamp()) {
                return true;
            }
            return false;
        }
        if ($type == Sign::TYPE_OUT) {
            if ($this->sign_out_start <= $now->getTimestamp() && $this->sign_out_end >= $now->getTimestamp()) {
                return true;
            }
            return false;
        }
        if ($this->allow_later_sign == 1) {
            //允许补签
            if ($type == Sign::TYPE_LATER_IN) {
                if ($this->supplementary_start <= $now->getTimestamp() && $this->supplementary_end >= $now->getTimestamp()) {
                    return true;
                }
                return false;
            }
        } else {
            return false;
        }
    }
}